package com.example.mynote.ui.clock;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mynote.R;

import java.util.ArrayList;
import java.util.Calendar;

class ClockAdapter extends RecyclerView.Adapter<ClockAdapter.myViewHolder> {

    private ArrayList<ClockBean> data;
    private Context context;


    public ClockAdapter(ArrayList<ClockBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    //***************************************************
    //自定义recyclerView的项目监听器
    private OnClickListener onClickListener;//接口对象

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
    /**
     * 定义点击事件回调接口.
     */
    public interface OnClickListener {
        /**
         * 点击事件.
         */
        void onClick(View itemView, int position);

        /**
         * 长点击事件.
         */
        void onLongClick(View itemView, int position);

        /**
         * 选项改变事件.
         */
        void onCheckedChange(View itemView, int position, boolean isChecked);
    }


    //***************************************************



    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.recyclerview_item,null);
        return new myViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
        holder.switchMsg.setChecked(data.get(position).getSelected());
        holder.remark.setText(data.get(position).getRemark());
        String h_str = data.get(position).getTime().get(Calendar.HOUR_OF_DAY)+"";
        if(h_str.length()==1) h_str = "0"+h_str;
        String m_str = data.get(position).getTime().get(Calendar.MINUTE)+"";
        if(m_str.length()==1) m_str = "0"+m_str;
        String str = h_str+":"+m_str; //hours:minute 格式
        holder.time.setText(str);
        final View itemView = holder.itemView; //整个列表项布局

//        ((myViewHolder)(holder)).updateItem();

//        itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // 设置单击事件并回调给页面
//                if (onClickListener != null) {
//                    onClickListener.onClick(itemView,holder.getLayoutPosition());
//                }
//            }
//        });
//        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                if (onClickListener != null) {
//                    onClickListener.onLongClick(itemView,holder.getLayoutPosition());
//                    return true;
//                }
//                return false;
//            }
//        });
//        holder.switchMsg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (onClickListener != null) {
//                    onClickListener.onClick(itemView,holder.getLayoutPosition());
//                }
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class myViewHolder extends RecyclerView.ViewHolder {
        private TextView time;
        private Switch switchMsg;
        private TextView remark;
        private LinearLayout background;

        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            time = (TextView)itemView.findViewById(R.id.timeMsg);
            switchMsg = (Switch) itemView.findViewById(R.id.switchMsg);
            remark = (TextView)itemView.findViewById(R.id.remark);
            background = (LinearLayout)itemView.findViewById(R.id.background);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 设置单击事件并回调给页面
                    if (onClickListener != null) {
                        onClickListener.onClick(itemView,getLayoutPosition());
                    }
                }
            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (onClickListener != null) {
                        onClickListener.onLongClick(itemView,getLayoutPosition());
                        return true;
                    }
                    return false;
                }
            });

            switchMsg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (onClickListener != null) {
                        onClickListener.onCheckedChange(itemView,getLayoutPosition(),switchMsg.isChecked());
                    }
                }
            });

//            switchMsg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
////                    Log.d("TAG", "onCheckedChanged: 点击了switch");
////                    Log.d("TAG", "onCheckedChanged: 点击了switch"+buttonView.getId());
//                }
//            });
        }

//        public void updateItem(){
//        }
    }

}
